import socket
import errno

from ..BaseIO import BaseIO

class NetlinxDMX(BaseIO):
    ui_name = 'Netlinx (AMX test module)'
    def __init__(self, **kwargs):
        super(NetlinxDMX, self).__init__(**kwargs)
        self.hostaddr = kwargs.get('hostaddr', '192.168.168.20')
        self.hostport = kwargs.get('hostport', 1998)
    def do_connect(self):
        self.do_disconnect()
        self.socket = socket.socket()
        self.socket.connect((self.hostaddr, self.hostport))
        self.connected = True
    def do_disconnect(self):
        if self.connected:
            self.socket.close()
        self.connected = False
    def _send(self, string):
        if self.connected:
            self.socket.send(string)
            print 'netlinx: ', string
    def sendDMXValue(self, **kwargs):
        universe = kwargs.get('universe')
        values = universe.values
        chan = kwargs.get('channel')
        
        if chan:
            self._send('C%s=%s!' % (chan, values[chan-1]))
            #print 'C%s=%s!' % (chan, values[chan])
        else:
            for chan, value in enumerate(values):
                self._send('C%s=%s!' % (chan, value))
                #print 'C%s=%s!' % (chan+1, value)
    
